package com.lottery.im.module.chat.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.lottery.im.R;
import com.lottery.im.api.result.entity.GroupNotice;
import com.lottery.im.base.BaseSwipeFragment;
import com.lottery.im.constant.Constants;
import com.lottery.im.constant.RxBusCode;
import com.lottery.im.module.chat.presenter.GroupNoticeDetailPresenter;
import com.lottery.im.module.chat.view.GroupNoticeDetailView;
import com.lottery.im.module.tim.model.GroupInfo;
import com.lottery.im.util.rxbus.RxBus;
import com.lottery.im.util.rxbus.Subscribe;
import com.tencent.imsdk.TIMGroupMemberRoleType;

/**
 * 公告详情
 */
public class GroupNoticeDetailFragment extends BaseSwipeFragment<GroupNoticeDetailPresenter> implements
    GroupNoticeDetailView {

  private final static String GROUPNOTICE_KEY = "GROUPNOTICE_KEY";
  private final static String POSITION_KEY = "POSITION_KEY";

  public static GroupNoticeDetailFragment create(GroupNotice groupNotice,int position) {
    GroupNoticeDetailFragment fragment = new GroupNoticeDetailFragment();
    Bundle b = new Bundle();
    b.putSerializable(GROUPNOTICE_KEY, groupNotice);
    b.putInt(POSITION_KEY,position);
    fragment.setArguments(b);
    return fragment;
  }

  @BindView(R.id.tv_title) TextView mTvTitle;
  @BindView(R.id.tv_notice_title) TextView mTvNoticeTitle;
  @BindView(R.id.tv_user_name) TextView mTvUserName;
  @BindView(R.id.tv_time) TextView mTvTime;
  @BindView(R.id.tv_del) TextView mTvDel;
  @BindView(R.id.tv_content) TextView mTvContent;
  @BindView(R.id.btn_edit) Button mBtnEdit;

  private GroupNotice mGroupNotice;

  @Override protected int getLayoutResId() {
    return R.layout.fragment_group_notice_detail;
  }

  @Override protected void initPresenter() {
    presenter = new GroupNoticeDetailPresenter(getActivity(),this,this);
    presenter.init();
  }

  @Override public void initView() {
    RxBus.get().register(this);
    mTvTitle.setText("公告详情");
    mGroupNotice = (GroupNotice) getArguments().getSerializable(GROUPNOTICE_KEY);
    String allContent = mGroupNotice.getContent();
    if (allContent.contains(Constants.NOTICE_SEPARATE)) {
      String title = allContent.substring(0, allContent.indexOf(Constants.NOTICE_SEPARATE));
      String content = allContent.substring(
          allContent.indexOf(Constants.NOTICE_SEPARATE) + Constants.NOTICE_SEPARATE.length(),
          allContent.length());
      mTvNoticeTitle.setText(title);
      mTvContent.setText(content);
    }else {
      mTvNoticeTitle.setText(allContent);
      mTvContent.setText(allContent);
    }
    mTvUserName.setText(mGroupNotice.getUSER_NAME());
    mTvTime.setText(mGroupNotice.getTime().substring(0,10));

    TIMGroupMemberRoleType role = GroupInfo.getInstance().getRole(mGroupNotice.getGroup_id());
    if (role == TIMGroupMemberRoleType.Owner || role == TIMGroupMemberRoleType.Admin) {
      mBtnEdit.setVisibility(View.VISIBLE);
      if (getArguments().getInt(POSITION_KEY) != 0){
        mTvDel.setVisibility(View.VISIBLE);
      }
    }

  }


  @OnClick({ R.id.iv_back, R.id.btn_edit , R.id.tv_del }) public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.iv_back:
        popBackStack();
        break;
      case R.id.btn_edit:
        startFragment(GroupNoticeEditFragment.create(mGroupNotice));
        break;
      case R.id.tv_del:
        presenter.del(mGroupNotice.getId());
        break;
    }
  }

  @Subscribe(code = RxBusCode.NOTICE_CODE)
  public void editSuc(String title){
    popBackStack();
  }

  @Override public void delSuc() {
    setFragmentResult(RESULT_OK,new Intent());
    popBackStack();
  }


  @Override public void onDestroy() {
    super.onDestroy();
    RxBus.get().unRegister(this);
  }


}
